<?php
class ModelCategory
{
	public function getParentCat()
	{
		$sql = "SELECT * FROM tbl_category where parent_id=0";
		
		$parent_cat_arr = DB::fetchAll($sql);
		return $parent_cat_arr;	
	}
	
	public function addCategory($form_values)
	{
		$cat_desc = $form_values['cat_desc'];
		$parent_id = $form_values['parent_id'];
		$status = $form_values['status'];
		
		$sql = "INSERT INTO tbl_category(cat_desc, parent_id, status)
				VALUES('" . $cat_desc . "', " . $parent_id . ", " . $status . ")";
		$add_category = DB::exec($sql);		
		if($add_category)
		{
			echo "<span class='success'>Category has been successfully added</span>";
		}
	}
	
	public function listCategories()
	{
		$sql = "SELECT * FROM tbl_category ";
               // echo $sql;
		$cat_arr = DB::fetchAll($sql);
		return $cat_arr;	
	}
        
        public function listCategoriesfordropdown()
	{
		$sql = "SELECT *
				FROM tbl_category
				WHERE parent_id = 0";
                //echo $sql;
		$cat_arr = DB::fetchAll($sql);
               // print_r($cat_arr);
		return $cat_arr;	
	}
	
	public function getCategoryByParentId($parentid)
	{
		$sql = "SELECT cat_id, cat_desc
				FROM tbl_category
				WHERE parent_id = " . $parentid;
		$parent_id = DB::fetchAll($sql);
		
		return $parent_id;
	}
        public function getcategorybysubcatid($subcatid) 
	{
		$sql = "SELECT parent_id
				FROM tbl_category
				WHERE cat_id = " . $subcatid;
		$id = DB::fetchRow($sql);

		return $id["parent_id"];
	}
        
         public function deletecategory($request)
	{
            //print_r($request);
            $dberror=0;
              
                DB::exec("SET AUTOCOMMIT=0");
                DB::exec("START TRANSACTION");

		
                 if($this->isparentcat($request["catid"])){
                     //echo "update  tbl_Products set product_cat_id=0 ,product_subcat_id=0 where cat_id =".$request["catid"]."";
                      $sqlupdate= DB::exec("update  tbl_products set product_cat_id=0 ,product_subcat_id=0 where product_cat_id =".$request["catid"]."");
                 }
                 else{ 
                     //echo "update  tbl_Products set product_subcat_id=0 where product_subcat_id =".$request["catid"]."";
                      $sqlupdate= DB::exec("update  tbl_products set product_subcat_id=0 where product_subcat_id =".$request["catid"]."");
                 }
                
                 if(mysql_error())
                    {
                    	$dberror=1;
                    }
                   // echo "1".$dberror.mysql_error();
                 $sqldelete =  DB::exec("delete from   tbl_category  where cat_id =".$request["catid"]." ");
                if(mysql_error())
                    {
                    	$dberror=1;
                    }
                $sqldeletesubcat =  DB::exec("delete from   tbl_category  where parent_id =".$request["catid"]."  ");
                if(mysql_error())
                    {
                    	$dberror=1;
                    }
                   // echo "2".$dberror.mysql_error();
                if($dberror==0)
                {
                     DB::exec("COMMIT");
                     echo "<span class='success'>Category has been successfully deleted</span>";
                }
                else
                {
                    DB::exec("ROLLBACK");
                    echo "<span class='error'>Update failed.Database error occured!</span>";
                }

              
               // exit;
		return $success;
	}
        
    public function isparentcat($catid){
        $sql=" select * from tbl_category where cat_id=".$catid." and parent_id=0 ";
        //echo $sql;
        $isparentcat= DB::fetchRow($sql);
        if($isparentcat){
            return true;
        }else{
            return false;
        }
            
    }
    
    public function renameCat($catid, $catlabel)
    {
    	$sql = "UPDATE tbl_category
    			SET cat_desc = '" . $catlabel . "'  
    			WHERE cat_id = " . $catid;
    	$rename_cat = DB::exec($sql);
    	
    	echo $catlabel;
    }
}
?>